﻿#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;

namespace yjy
{
	class string
	{
	public:
		//迭代器
		using iterator = char*;
		using const_iterator = const char*;
		//构造
		string(const char* str = "");
		string(const string& s);
		string& operator=(string s);
		//析构
		~string();

		//字符串的大小
		size_t size() { return _size; }
		//字符串的容量
		size_t capacity() { return _capacity; }
		//字符串的判空
		bool empty()
		{
			if (_str == nullptr) return true;
			return false;
		}
		//
		string substr(size_t pos, size_t len = npos);//从pos开始拷贝len个字符
		//插入
		void push_back(char s);
		void append(const char* str);
		void insert(int pos, char s);
		void insert(int pos, const char* str);
		string& operator+=(char s);
		string& operator+=(const char* str);
		//删除
		void pop_back();
		void erase(size_t pos, size_t len = npos);//删除指定位置的字符
		//交换
		void swap(char* str);
		//扩容
		void reserve(size_t n);
		//查找
		size_t find(char ch, size_t pos = 0);//从pos位置开始查找
		size_t find(const char* str, size_t pos = 0);
		// []
		char& operator[](size_t i)
		{
			assert(i < _size);

			return _str[i];
		}
		const char& operator[](size_t i) const
		{
			assert(i < _size);
			return _str[i];
		}
		//
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}
		size_t size() const
		{
			return _size;
		}
		
		const char* c_str() const
		{
			return _str;
		}
		//清空
		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
		//交换
		void swap(string& s);

	private:
		char* _str = nullptr;//指针,指向对应的字符数组
		size_t _size = 0;//字符串的大小
		size_t _capacity = 0;//容量
	public:
		// ⴦
		//static const size_t npos = -1;

		static const size_t npos;
	};
	void swap(string& s1, string& s2);

	bool operator==(const string& lhs, const string& rhs);
	bool operator!=(const string& lhs, const string& rhs);
	bool operator>(const string& lhs, const string& rhs);
	bool operator<(const string& lhs, const string& rhs);
	bool operator>=(const string& lhs, const string& rhs);
	bool operator<=(const string& lhs, const string& rhs);
	
	ostream& operator<<(ostream & os, const string & str);
	istream& operator>>(istream& is, string& str);
	istream& operator>>(istream& is, string& str);
	//整行读取
	istream& getline(istream& is, string& str, char delim = '\n');
}


